﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ConfigUtilLib;
using DataFetcherLib;
using DataProcessorLib;
using log4net;
using log4net.Config;

namespace realestateAnalyticsBot
{
    class PropertyBot
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {
            // 1.  Check Arguments 
            // <AppName> <ConfigFilePath> 
            // <AppName>  help , no args is Help only 
            // Log4NET Configuration 
            BasicConfigurator.Configure();

            if (args.Length  == 0 )
            {
                // No Args , Handling helping with nice print out 
                log.FatalFormat(" Usage : <appName>  configfilePath [Optional : debugConfigLog4j]");
                return;
            }

            if (args.Length == 2)
            {
                log.FatalFormat("Configuring as per new log4NET config file {0}", args[1]);
                XmlConfigurator.Configure(new System.IO.FileInfo(args[1]));
            }

            AppConfig appConfig = AppConfig.readConfiguration(args[0]);
            if (appConfig == null)
            {
                log.FatalFormat("Failed to read Configuration :{0} ", args[0]);
                return;
            }

            log.DebugFormat("Application : Completed reading Configuration ...");

            // Get Singleton Objects of Fetcher Manager and Data Processing Manager.
            FetcherManager fetchermgr = FetcherManager.getDatafetcherSingletonObject();
            DataProcessManager dataprocessMgr = DataProcessManager.getDatafetcherSingletonObject();

            log.DebugFormat("Application : Kick Off Fetcher ");
            fetchermgr.KickOffDataFetcher(appConfig);

            log.DebugFormat("Application : Kick off Processor ");
            // Kick off Application processor Thread. 
            dataprocessMgr.kickOffProcessingData(appConfig);

            log.DebugFormat(" Completed the  Job .. Exiting Bye Bye ");

        }
    }
}
